/*package com.ruoyi.web.controller.jiugongge;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.web.base.BaseController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/linksucai"})
public class LinksucaiController
  extends BaseController
{
  private static final Logger logger = Logger.getLogger(LinksucaiController.class);
  @Autowired
  private JwSystemProjectService jwSystemProjectService;
  private static String SIGN_KEY = "";
  private static final String SCOPE = "snsapi_base";
  private static String domain = "";
  
  static
  {
    PropertiesUtil oAuth = new PropertiesUtil("p3-web-sysconfig.properties");
    SIGN_KEY = oAuth.readProperty("oAuthSignKey");
    domain = oAuth.readProperty("oAuthDomain");
  }
  
  public static void setDomain(String newdomain)
  {
    domain = newdomain;
  }
  
  public static String getDomain()
  {
    return domain;
  }
  
  @RequestMapping(value={"link"}, method={org.springframework.web.bind.annotation.RequestMethod.GET, org.springframework.web.bind.annotation.RequestMethod.POST})
  public void link(HttpServletRequest request, HttpServletResponse response)
  {
    long start = System.currentTimeMillis();
    logger.info("-------[LINKSTARTIME]--------------����������------------>" + start);
    try
    {
      String linkid = request.getParameter("linkid");
      
      String jwid = request.getParameter("jwid");
      String state = request.getParameter("state");
      
      WeixinAccount weixinAccount = JedisPoolUtil.getWxAccount(jwid);
      if (weixinAccount == null)
      {
        logger.error("------[����]----------OpenWX----------��������ID��������,��������������������ID: ------" + jwid);
        return;
      }
      String code = request.getParameter("code");
      String APPID = weixinAccount.getAccountappid();
      String APPSECRET = weixinAccount.getAccountappsecret();
      if ((code == null) || (code.length() == 0))
      {
        logger.info("-------[LINKWXSTEP1_START]-----------������������������--------------->" + (System.currentTimeMillis() - start) + "ms");
        String web_oauth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        String REDIRECT_URI = request.getRequestURI();
        if (REDIRECT_URI.indexOf(domain) == -1) {
          REDIRECT_URI = domain.replace(request.getContextPath(), "") + REDIRECT_URI;
        }
        logger.info("------------------REDIRECT_URI--------1---------" + REDIRECT_URI);
        REDIRECT_URI = REDIRECT_URI + "?" + request.getQueryString();
        logger.info("------------------REDIRECT_URI--------2---------" + REDIRECT_URI);
        REDIRECT_URI = URLEncoder.encode(REDIRECT_URI, "UTF-8");
        String weixinOauthURL = web_oauth_url.replace("APPID", APPID).replace("REDIRECT_URI", REDIRECT_URI).replace("SCOPE", "snsapi_base");
        if (oConvertUtils.isNotEmpty(state)) {
          weixinOauthURL = weixinOauthURL.replace("STATE", state);
        }
        logger.info("------------------weixinOauthURL-----------------" + weixinOauthURL);
        logger.info("-------[LINKWXSTEP1_END]--------------������������������------------>" + (System.currentTimeMillis() - start) + "ms");
        response.sendRedirect(weixinOauthURL);
      }
      else
      {
        logger.info("-------[LINKWXSTEP2_START]------------������������������-------------->" + (System.currentTimeMillis() - start));
        
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        requestUrl = requestUrl.replace("APPID", APPID);
        requestUrl = requestUrl.replace("SECRET", APPSECRET);
        requestUrl = requestUrl.replace("CODE", code);
        logger.info("------------------code-----------------" + code);
        
        logger.info("-------[LINKWXSTEP2_REQUEST]��������oauth2��������openid����-------------------------->" + (System.currentTimeMillis() - start) + "ms");
        JSONObject json2 = httpRequest(requestUrl, "POST", null);
        logger.info("-------[LINKWXSTEP2_RESPONSE]��������oauth2��������openid����-------------------------->" + (System.currentTimeMillis() - start) + "ms");
        
        String openid = (String)json2.get("openid");
        logger.info("----------------get--openid-----------------" + openid);
        
        String jeewxUrl = domain + "/" + this.jwSystemProjectService.queryById(linkid).getHdzsUrl();
        if (oConvertUtils.isEmpty(jeewxUrl))
        {
          logger.error("------[����]----------OpenWX----------��������ID��������,��������������Link ID: ------" + linkid);
          return;
        }
        logger.info("----------------outerLink------1-----------" + jeewxUrl);
        if (oConvertUtils.isNotEmpty(jeewxUrl))
        {
          jeewxUrl = jeewxUrl.replace("${openid}", openid);
          jeewxUrl = jeewxUrl.replace("${wxid}", weixinAccount.getWeixinAccountid());
          jeewxUrl = jeewxUrl.replace("${wxcode}", weixinAccount.getAccountnumber());
          jeewxUrl = jeewxUrl.replace("${appid}", weixinAccount.getAccountappid());
          jeewxUrl = jeewxUrl.replace("${appsecret}", weixinAccount.getAccountappsecret());
          jeewxUrl = jeewxUrl.replace("${accesstoken}", weixinAccount.getAccountaccesstoken());
          
          String utoken = oConvertUtils.getString(json2.get("access_token"));
          jeewxUrl = jeewxUrl.replace("${utoken}", utoken);
        }
        logger.info("----------------request.getQueryString()-------2----------" + request.getQueryString());
        String queryParam = request.getQueryString();
        if (oConvertUtils.isNotEmpty(queryParam)) {
          jeewxUrl = jeewxUrl + "&" + queryParam;
        }
        logger.info("----------------outerLink-------3----------" + jeewxUrl);
        
        String sign = SignatureUtil.sign(getSignMap(jeewxUrl), SIGN_KEY);
        logger.info("-------[LINKWXSTEP2_END]--------------������������������------------>" + (System.currentTimeMillis() - start) + "ms");
        response.sendRedirect(jeewxUrl + "&sign=" + sign);
      }
    }
    catch (Exception localException) {}
  }
  
  private JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr)
  {
    jsonObject = null;
    StringBuffer buffer = new StringBuffer();
    HttpURLConnection httpUrlConn = null;
    try
    {
      URL url = new URL(requestUrl);
      httpUrlConn = (HttpURLConnection)url.openConnection();
      httpUrlConn.setDoOutput(true);
      httpUrlConn.setDoInput(true);
      httpUrlConn.setUseCaches(false);
      
      httpUrlConn.setConnectTimeout(4500);
      httpUrlConn.setReadTimeout(4500);
      
      httpUrlConn.setRequestMethod(requestMethod);
      if ("GET".equalsIgnoreCase(requestMethod)) {
        httpUrlConn.connect();
      }
      if (null != outputStr)
      {
        OutputStream outputStream = httpUrlConn.getOutputStream();
        
        outputStream.write(outputStr.getBytes("UTF-8"));
        outputStream.close();
      }
      InputStream inputStream = httpUrlConn.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
      
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      
      String str = null;
      while ((str = bufferedReader.readLine()) != null) {
        buffer.append(str);
      }
      bufferedReader.close();
      inputStreamReader.close();
      
      inputStream.close();
      inputStream = null;
      httpUrlConn.disconnect();
      return JSONObject.parseObject(buffer.toString());
    }
    catch (ConnectException localConnectException) {}catch (Exception localException1) {}finally
    {
      try
      {
        httpUrlConn.disconnect();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
  
  private Map<String, String> getSignMap(String url)
  {
    Map<String, String> paramMap = new HashMap();
    url = url.substring(url.indexOf("?") + 1);
    String[] params = url.split("&");
    for (int i = 0; i < params.length; i++)
    {
      String param = params[i];
      if (param.indexOf("=") != -1)
      {
        String[] values = param.split("=");
        if ((values != null) && (values.length == 2)) {
          if ("nickname".equals(values[0])) {
            paramMap.put(values[0], URLDecoder.decode(values[1]));
          } else {
            paramMap.put(values[0], values[1]);
          }
        }
      }
    }
    return paramMap;
  }
}

*/